<template>
	<view class="flex-col homepage">
		<u-form-item class="formItemInput" label="住户姓名" :label-width="70">
			<u-input placeholder="请输入真实姓名" v-model="this.user.userName" type="text" readonly="readonly"></u-input>
		</u-form-item>
		<u-form-item class="formItemActionSheet" label="住户性别" :label-width="70">
			<u-input placeholder="请输入" v-model="this.user.sex" type="text" readonly="readonly"></u-input>
		</u-form-item>
		<u-form-item class="formItemActionSheet" label="身份证号" :label-width="70">
			<u-input placeholder="请输入" v-model="this.user.idCard" type="text" readonly="readonly"></u-input>
		</u-form-item>
		<u-form-item class="formItemActionSheet" label="与业主关系" :label-width="80">
			<u-input placeholder="请输入" v-model="this.user.relationShip" type="text" readonly="readonly"></u-input>
		</u-form-item>
		<u-form-item class="formItemInput-two" label="邮箱号码" :label-width="70">
			<u-input placeholder="请输入手机号码" v-model="user.email" type="text" readonly="readonly"></u-input>
			<u-tag text="发送验证码" type="success" @click="fsyzm()"></u-tag>
		</u-form-item>
		<u-form-item class="formItemInput-two" label="验证码" :label-width="70">
			<u-input placeholder="请输入六位验证码" v-model="test" type="text"></u-input>
		</u-form-item>

		<view class="">
			<u--text type="primary" text="上传本人身份证照片">上传本人身份证照片</u--text>
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10">
			</u-upload>
		</view>
		<u-button type="primary" text="提交审核" style="width: 300px;" @click="SubmitAudit"></u-button>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				fileList1: [],
				form:{},
				user: {},
				carNumber: false,
				tttt: false,
				range: [{
						value: 1,
						text: "男"
					},
					{
						value: 2,
						text: "女"
					}
				],
				yzm: '',
				test: ''
			}

		},
		onLoad(e) {
			this.form = JSON.parse(decodeURIComponent(e.data));
			this.user = uni.getStorageSync('user')
		},
		methods: {
			change() {},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				this.user = uni.getStorageSync('user')

				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://127.0.0.1:9000/serverwg/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test',
							id: this.user.userId
						},
						success: (res) => {
							if (res.data != null) {
								this.find(res.data)
							} else {
								alert("网络原因，上传失败")
							}
						}
					});
				})
			},
			find(file) {
				this.$axios({
					url: 'serverwg/card1',
					data: {
						url1: file
					},
					method: 'GET',
					success: (res) => {
						if (res.data.code == 20000) {
							alert("身份证验证通过")
						}
					}
				})
			},
			fsyzm() {
				this.$axios({
					url: 'serverwg/fsyzm',
					data: {
						email: this.user.email
					},
					method: 'GET',
					success: (res) => {
						if (res.data.code == 20000) {
							alert("发送成功")
							console.log(res)
							this.yzm = res.data.data.yzm
						}
					}
				})
			},
			SubmitAudit() {
				if (this.test == '') {
					alert("验证码为空！！！")
				}
				if (this.test == this.yzm) {
					alert("验证码正确")
					this.$axios({
						url: 'serverwg/addCellIntermediate',
						data: this.form,
						method: 'POST',
						success: (res) => {
							if (res.data.code == 20000) {
								alert("添加成功")
								uni.navigateTo({
								    url: '../myHome',
								    success: function(res) {              
								    }
								})
							}
						}
					})
				} else {
					alert("验证码错误！！！")
				}
				
			}


		}
	}
</script>
<style lang="scss" scoped>
	/** 全局样式-开始,建议放在公共css样式文件中,比如common.css 或app.vue文件 */

	html,
	body {
		margin: 0;
		width: 100%;
		height: 100%;
		font-size: 16px
	}

	view,
	text,
	image {
		position: relative;
		box-sizing: border-box;
		flex-shrink: 0;
	}

	.flex-col {
		display: flex;
		align-items: flex-start;
		flex-direction: column;
	}

	.flex-row {
		display: flex;
		align-items: flex-start;
	}

	.flex-col .flex-row {
		width: 100%
	}

	/** 全局样式-结束*/

	.homepage {
		height: 1623.188rpx;
		padding: 28.986rpx 0rpx 0rpx 1.812rpx;
		border-color: #ff9900;

		.formItemInput {
			width: 100%;
		}

		.formItemActionSheet {
			width: 100%;
		}

		.formItemInput-two {
			width: 100%;
			margin-bottom: 12.681rpx;
		}

		.wrapper {
			width: 262.681rpx;
			justify-content: space-between;
			margin-bottom: 21.739rpx;

			.text {
				width: 114.13rpx;
				margin-top: 14.493rpx;
			}
		}
	}
</style>
